package cn.wuxing.mediation.infoconfig.service.impl;

import cn.wuxing.mediation.common.core.domain.TreeSelect;
import cn.wuxing.mediation.common.core.domain.model.TownshipCenterPOJO;
import cn.wuxing.mediation.common.utils.StringUtils;
import cn.wuxing.mediation.infoconfig.mapper.IcTownshipCenterMapper;
import cn.wuxing.mediation.infoconfig.service.IIcTownshipCenterService;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 调解委员会管理 服务实现
 *
 * @author mediation
 */
@Service
public class IcTownshipCenterServiceImpl implements IIcTownshipCenterService {

    @Autowired
    private IcTownshipCenterMapper townshipCenterMapper;

    @Override
    public List<TreeSelect> selectTreeList(String name) {
        List<TownshipCenterPOJO> list = this.townshipCenterMapper.queryList(name);
        return buildTreeSelect(list);
    }


    public List<TownshipCenterPOJO> buildMediatorCommitteeTree(List<TownshipCenterPOJO> townshipCenterVOS) {
        List<TownshipCenterPOJO> returnList = new ArrayList<TownshipCenterPOJO>();
        List<Long> tempList = townshipCenterVOS.stream().map(TownshipCenterPOJO::getId).collect(Collectors.toList());
        for (TownshipCenterPOJO townshipCenterVO : townshipCenterVOS) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(townshipCenterVO.getPreId())) {
                recursionFn(townshipCenterVOS, townshipCenterVO);
                returnList.add(townshipCenterVO);
            }
        }
        if (returnList.isEmpty()) {
            returnList = townshipCenterVOS;
        }
        return returnList;
    }


    public List<TreeSelect> buildTreeSelect(List<TownshipCenterPOJO> townshipCenterVOS) {
        List<TownshipCenterPOJO> trees = buildMediatorCommitteeTree(townshipCenterVOS);
        return trees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    /**
     * 递归列表
     */
    private void recursionFn(List<TownshipCenterPOJO> list, TownshipCenterPOJO t) {
        // 得到子节点列表
        List<TownshipCenterPOJO> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TownshipCenterPOJO tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<TownshipCenterPOJO> getChildList(List<TownshipCenterPOJO> list, TownshipCenterPOJO t) {
        List<TownshipCenterPOJO> tlist = new ArrayList<TownshipCenterPOJO>();
        Iterator<TownshipCenterPOJO> it = list.iterator();
        while (it.hasNext()) {
            TownshipCenterPOJO n = (TownshipCenterPOJO) it.next();
            if (StringUtils.isNotNull(n.getPreId()) && n.getPreId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TownshipCenterPOJO> list, TownshipCenterPOJO t) {
        return getChildList(list, t).size() > 0;
    }


}
